package com.china.fangxinwallet;
import androidx.annotation.NonNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class MainActivity extends FlutterActivity {
    protected MethodChannel methodChannel;

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        methodChannel = new MethodChannel(flutterEngine.getDartExecutor(), "native");
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("ENVIRONMENT", BuildConfig.ENVIRONMENT);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        methodChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(@NonNull MethodCall methodCall, @NonNull MethodChannel.Result result) {
                switch (methodCall.method) {
                    case "ENVIRONMENT":
                        result.success(jsonObject.toString());
                        break;
                    case "createEvent" :
                        String title = methodCall.argument("title");
                        String note = methodCall.argument("note");
                        List<Integer> alert = methodCall.argument("alert");
                        Long beginTime = methodCall.argument("beginTime");
                        Long endTime = methodCall.argument("endTime");
                        String eventId = methodCall.argument("eventId");
                        int allDay = methodCall.argument("allDay");
                        Calendars calendars = new Calendars(title,note,alert,eventId,beginTime,endTime,allDay);
                        String resAdd = null;
                        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                            resAdd = CalendarProviderUtil.addEvent(MainActivity.this,calendars);
                        }
                        result.success(resAdd);
                        break;

                }
            }
        });
    }

}
